package com.yuandian.business.monitor.controller;

import com.yuandian.api.business_monitor.po.CockpitParams;
import com.yuandian.api.business_monitor.vo.*;
import com.yuandian.api.management.feign.RemoteAlarmService;
import com.yuandian.base.service.ITaskDataService;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.business.monitor.service.ICockpitService;
import com.yuandian.vo.TrendDataVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 说明：首页大屏数据驾驶舱Controller
 *
 * @Author yanjun
 * @Date 2020/9/25 13:43
 */
@RestController
@Api(value = "首页大屏数据驾驶舱接口", tags = "首页大屏数据驾驶舱接口")
@RequestMapping("/home/cockpit")
public class CockpitController {


	@Autowired
	private ICockpitService cockpitService;

	@Autowired
	private ITaskDataService taskDataService;
	@Autowired
	private RemoteAlarmService remoteAlarmService;

	/**
	 * 首页大屏 点击‘展开’ 查询指定系统一整天的 请求量, 告警量, 失败量
	 *
	 * @param params 首页大屏数据驾驶舱搜索条件
	 * @return
	 */
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId", 			value = "系统id", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "endTime", 			value = "结束时间", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "startTime", 			value = "开始时间", 			paramType = "query", 	required = true, 	dataType = "Long"),
			@ApiImplicitParam(name = "currentDate",   			value = "当前时间", 				paramType = "query", 	required = true, 	dataType = "Long"),
	})
	@PostMapping("/allTrendList")
	@ApiOperation("首页大屏 点击‘展开’ 查询指定系统一整天的 请求量, 告警量, 失败量")
	public R allTrendList(@RequestBody CockpitParams params) {
		// 避免页面传 systemIdList 参数
		params.setSystemIdList(null);

		List<RequestTrendVo> requestList = cockpitService.allTrendList(params);

		TrendDataVo trendDataVo = taskDataService.getTrendObject("详单表分钟统计", params.getStartTime(), params.getEndTime(), requestList);
		return R.ok(trendDataVo);
	}

	/**
	 * 查询系统总览
	 *
	 * @param params 首页大屏数据驾驶舱搜索条件
	 * @return 首页大屏-系统总览vo对象
	 */
	@ApiImplicitParams({
			@ApiImplicitParam(name = "endTime", 			value = "结束时间", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "startTime", 			value = "开始时间", 			paramType = "query", 	required = true, 	dataType = "Long"),
			@ApiImplicitParam(name = "currentDate",   			value = "当前时间", 				paramType = "query", 	required = true, 	dataType = "Long"),
	})
	@ApiOperation("查询系统总览")
	@PostMapping("/systemOverviewList")
	public R systemOverviewList(@RequestBody CockpitParams params) {
		// 调用服务
		List<SystemOverview> list = cockpitService.systemOverview(params);
		return R.ok(list);
	}

	/**
	 * 查询失败量趋势
	 * 失败量趋势-busi_sys_visit_trend_m-DetailMinuteStatTask-详单表分钟统计-SystemVisitTrendStatHandler-延迟3分钟-modules/home/cockpit/failTrendList
	 *
	 * @param params 首页大屏数据驾驶舱搜索条件
	 * @return 首页大屏-失败量趋势vo对象
	 */
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId", 			value = "系统id", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "systemIdList", 			value = "系统id", 			paramType = "query", 	required = true, 	dataType = "Array"),
			@ApiImplicitParam(name = "endTime", 			value = "结束时间", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "startTime", 			value = "开始时间", 			paramType = "query", 	required = true, 	dataType = "Long"),
			@ApiImplicitParam(name = "currentDate",   			value = "当前时间", 				paramType = "query", 	required = true, 	dataType = "Long"),
	})
	@ApiOperation("查询失败量趋势")
	@PostMapping("/failTrendList")
	public R failTrendList(@RequestBody CockpitParams params) {
		// 调用服务
		FailTrendVo failTrendVo = cockpitService.failTrend(params);
		TrendDataVo trendDataVo = taskDataService.getTrendObject("ads_busi_sys_visit_trend_m", params.getStartTime(), params.getEndTime(), failTrendVo);
		return R.ok(trendDataVo);
	}

	/**
	 * 查询系统业务访问量TopN
	 *
	 * @param params 首页大屏数据驾驶舱搜索条件
	 * @return 首页大屏-系统业务访问量TopN vo对象
	 */
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId", 			value = "系统id", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "systemIdList", 			value = "系统id", 			paramType = "query", 	required = true, 	dataType = "Array"),
			@ApiImplicitParam(name = "endTime", 			value = "结束时间", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "startTime", 			value = "开始时间", 			paramType = "query", 	required = true, 	dataType = "Long"),
			@ApiImplicitParam(name = "currentDate",   			value = "当前时间", 				paramType = "query", 	required = true, 	dataType = "Long"),
	})
	@PostMapping("/businessVisitsTopNList")
	@ApiOperation("查询系统业务访问量TopN")
	public R businessVisitsTopNList(@RequestBody CockpitParams params) {
		List<BusinessVisitsTopNVo> list = cockpitService.businessVisitsTopN(params);
		return R.ok(list);
	}

	/**
	 * 查询系统业务健康度TopN
	 *
	 * @param params 首页大屏数据驾驶舱搜索条件
	 * @return 首页大屏-系统业务健康度TopN vo对象
	 */
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId", 			value = "系统id", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "systemIdList", 			value = "系统id", 			paramType = "query", 	required = true, 	dataType = "Array"),
			@ApiImplicitParam(name = "endTime", 			value = "结束时间", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "startTime", 			value = "开始时间", 			paramType = "query", 	required = true, 	dataType = "Long"),
			@ApiImplicitParam(name = "currentDate",   			value = "当前时间", 				paramType = "query", 	required = true, 	dataType = "Long"),
	})
	@PostMapping("/businessHealthyTopNList")
	@ApiOperation("查询系统业务健康度TopN")
	public R businessHealthyTopNList(@RequestBody CockpitParams params) {
		List<BusinessHealthyTopNVo> list = cockpitService.businessHealthyTopN(params);
		return R.ok(list);
	}

	/**
	 * 查询系统请求量趋势
	 * 请求量趋势-busi_sys_req_trend_m-SystemReqTrendStatTask-系统请求量趋势统计-延迟3分钟-modules/home/cockpit/requestTrendList
	 *
	 * @param params 首页大屏数据驾驶舱搜索条件
	 * @return 首页大屏-系统请求量趋势vo对象
	 */
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId", 			value = "系统id", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "systemIdList", 			value = "系统id", 			paramType = "query", 	required = true, 	dataType = "Array"),
			@ApiImplicitParam(name = "endTime", 			value = "结束时间", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "startTime", 			value = "开始时间", 			paramType = "query", 	required = true, 	dataType = "Long"),
			@ApiImplicitParam(name = "currentDate",   			value = "当前时间", 				paramType = "query", 	required = true, 	dataType = "Long"),
	})
	@PostMapping("/requestTrendList")
	@ApiOperation("查询系统请求量趋势")
	public R requestTrendList(@RequestBody CockpitParams params) {
		// 调用服务
		RequestTrend requestTrend = cockpitService.requestTrend(params);
		TrendDataVo trendDataVo = taskDataService.getTrendObject("ads_busi_sys_req_trend_m", params.getStartTime(), params.getEndTime(), requestTrend);
		return R.ok(trendDataVo);
	}


	/**
	 * 查询系统网端告警TopN
	 *
	 * @param params 首页大屏数据驾驶舱搜索条件
	 * @return 首页大屏-系统网端告警TopN vo对象
	 */
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId", 			value = "系统id", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "systemIdList", 			value = "系统id", 			paramType = "query", 	required = true, 	dataType = "Array"),
			@ApiImplicitParam(name = "endTime", 			value = "结束时间", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "startTime", 			value = "开始时间", 			paramType = "query", 	required = true, 	dataType = "Long"),
			@ApiImplicitParam(name = "currentDate",   			value = "当前时间", 				paramType = "query", 	required = true, 	dataType = "Long"),
	})
	@PostMapping("/segmentAlarmTopNList")
	@ApiOperation("查询系统网端告警TopN")
	public R segmentAlarmTopNList(@RequestBody CockpitParams params) {
		// 调用服务
		List<SegmentAlarmTopNVo> list = cockpitService.segmentAlarmTopN(params);
		return R.ok(list);
	}

	/**
	 * 查询系统告警趋势
	 * 告警量趋势-busi_alarm_trend_m-BusinessAlarmTrendTask-系统告警趋势统计-BusinessAlarmTrendHandler-延迟3分钟-modules/home/cockpit/systemAlarmTrendList
	 *
	 * @param params 首页大屏数据驾驶舱搜索条件
	 * @return 首页大屏-系统告警趋势vo对象
	 */
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId", 			value = "系统id", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "systemIdList", 			value = "系统id", 			paramType = "query", 	required = true, 	dataType = "Array"),
			@ApiImplicitParam(name = "endTime", 			value = "结束时间", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "startTime", 			value = "开始时间", 			paramType = "query", 	required = true, 	dataType = "Long"),
			@ApiImplicitParam(name = "currentDate",   			value = "当前时间", 				paramType = "query", 	required = true, 	dataType = "Long"),
	})
	@PostMapping("/systemAlarmTrendList")
	@ApiOperation("查询系统告警趋势")
	public R systemAlarmTrendList(@RequestBody CockpitParams params) {
		List<Integer> l = remoteAlarmService.queryIndicator("BPM").getData();
		params.setIndicators(l);
		// 调用服务
		SystemAlarmTrend systemAlarmTrend = cockpitService.systemAlarmTrend(params);
		/**
		 * 将任务的任务名称、查询的开始时间、查询的结束时间，趋势图数组或者对象传到此方法里，此方法会计算趋势图的开始时间、结束时间，
		 * 并返回一个包装类，不会对趋势图数组或者对象进行加工
		 */
		TrendDataVo trendDataVo = taskDataService.getTrendObject("ads_busi_alarm_trend_m", params.getStartTime(), params.getEndTime(), systemAlarmTrend);
		return R.ok(trendDataVo);
	}

	/**
	 * 查询高级告警信息列表
	 *
	 * @return 首页大屏-高级告警信息vo对象集合
	 */
//	@GetMapping("/advancedAlarmInfoList")
//	public R advancedAlarmInfoList() {
////		List<AdvancedAlarmInfoVo> list = cockpitService.advancedAlarmInfoList();
//		return R.ok();
//	}

	/**
	 * 查询流量变化趋势
	 *
	 * @return 首页大屏-流量变化趋势
	 */
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId", 			value = "系统id", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "systemIdList", 			value = "系统id", 			paramType = "query", 	required = true, 	dataType = "Array"),
			@ApiImplicitParam(name = "endTime", 			value = "结束时间", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "startTime", 			value = "开始时间", 			paramType = "query", 	required = true, 	dataType = "Long"),
			@ApiImplicitParam(name = "currentDate",   			value = "当前时间", 				paramType = "query", 	required = true, 	dataType = "Long"),
	})
	@PostMapping("/queryFlowTrend")
	@ApiOperation("首页大屏-流量变化趋势")
	public R queryFlowTrend(@RequestBody CockpitParams params) {
		FlowTrendVo flowTrend = cockpitService.queryFlowTrend(params);
		TrendDataVo trendDataVo = taskDataService.getTrendObject("ads_busi_alarm_trend_m", params.getStartTime(), params.getEndTime(), flowTrend);
		return R.ok(trendDataVo);
	}
	/**
	 * 查询业务办理时长趋势
	 *
	 * @return 首页大屏-业务办理时长趋势
	 */
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId", 			value = "系统id", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "systemIdList", 			value = "系统id", 			paramType = "query", 	required = true, 	dataType = "Array"),
			@ApiImplicitParam(name = "endTime", 			value = "结束时间", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "startTime", 			value = "开始时间", 			paramType = "query", 	required = true, 	dataType = "Long"),
			@ApiImplicitParam(name = "currentDate",   			value = "当前时间", 				paramType = "query", 	required = true, 	dataType = "Long"),
	})
	@ApiOperation("首页大屏-业务办理时长趋势")
	@PostMapping("/queryBusinessHandleTrend")
	public R queryBusinessHandleTrend(@RequestBody CockpitParams params) {
		BusHandleTrendVo busHandleTrendVo = cockpitService.queryBusinessHandleTrend(params);
		TrendDataVo trendDataVo = taskDataService.getTrendObject("查询业务办理时长趋势", params.getStartTime(), params.getEndTime(), busHandleTrendVo);
		return R.ok(trendDataVo);
	}

	/**
	 * 网端流量top10
	 * todo   需要数据底座数据入库
	 * @return 首页大屏-网端流量top10
	 */
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId", 			value = "系统id", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "systemIdList", 			value = "系统id", 			paramType = "query", 	required = true, 	dataType = "Array"),
			@ApiImplicitParam(name = "endTime", 			value = "结束时间", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "startTime", 			value = "开始时间", 			paramType = "query", 	required = true, 	dataType = "Long"),
			@ApiImplicitParam(name = "currentDate",   			value = "当前时间", 				paramType = "query", 	required = true, 	dataType = "Long"),
	})
	@PostMapping("/querySegmentFlowTop")
	@ApiOperation("首页大屏-网端流量top10占比")
	public R querySegmentFlowTop(@RequestBody CockpitParams params) {
		List<SegmentFlowTopVo> list = cockpitService.querySegmentFlowTop(params);

//		BusHandleTrendVo busHandleTrendVo = cockpitService.querySegmentFlowTop(params);
//		TrendDataVo trendDataVo = taskDataService.getTrendObject("查询业务办理时长趋势", params.getStartTime(), params.getEndTime(), busHandleTrendVo);
		return R.ok(list);
	}

	/**
	 * 网端请求top10
	 *
	 * @return 首页大屏-网端请求top10占比
	 */
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId", 			value = "系统id", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "systemIdList", 			value = "系统id", 			paramType = "query", 	required = true, 	dataType = "Array"),
			@ApiImplicitParam(name = "endTime", 			value = "结束时间", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "startTime", 			value = "开始时间", 			paramType = "query", 	required = true, 	dataType = "Long"),
			@ApiImplicitParam(name = "currentDate",   			value = "当前时间", 				paramType = "query", 	required = true, 	dataType = "Long"),
	})
	@PostMapping("/querySegmentReqTop")
	@ApiOperation("首页大屏-网端请求top10")
	public R querySegmentReqTop(@RequestBody CockpitParams params) {
		List<SegmentReqTopNVo> segmentReqTopNVo = cockpitService.querySegmentReqTop(params);
		return R.ok(segmentReqTopNVo);
	}


}
